package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.TogetherTrip;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TogetherTripQueryObject;
import cn.wolfcode.trip.base.service.ITogetherTripService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/togetherTrips")
public class TogetherTripController {

    @Autowired
    private ITogetherTripService togetherTripService;



    /**
     * 获取全部结伴内容
     * 请求路径  /togetherTrips
     * 请求方式  get
     * 请求参数  TogetherTripQueryObject
     * 返回结果  PageInfo
     */
    @GetMapping
    public PageInfo query(TogetherTripQueryObject qo) {
        // 已发布的，已发布的就是已公开的
        qo.setState(true);
        qo.setOrderBy("tt.releaseTime DESC");
        return togetherTripService.query(qo);
    }

    @GetMapping("{id}")
    public TogetherTrip getById(@PathVariable Long id) {
        return togetherTripService.getById(id);
    }
    /**
     * 请求保存结伴的接口
     * 请求路径	/travels
     * 请求方式	post
     * 请求参数	结伴对象
     * 返回结果	JsonResult
     */
    @PostMapping
    public JsonResult save(TogetherTrip togetherTrip) {
        togetherTrip.setCollectionNum(0);
        togetherTrip.setApplyNum(0);
        togetherTrip.setReleaseTime(new Date());
        togetherTripService.save(togetherTrip);
        return new JsonResult();
    }


    //查询该结伴有改用户报名没
    @GetMapping("{togetherId}/applys/{userId}")
    public JsonResult selectTogetherByUserId(@PathVariable Long togetherId,@PathVariable Long userId) {
        TogetherTrip togetherTrip = togetherTripService.selectTogetherByUserId(togetherId,userId);
        JsonResult result = new JsonResult();
        if (togetherTrip != null) {
            return new JsonResult();
        }
        result.mark("炸了");
        return result;
    }
    //查询该结伴有改用户关注没
    @GetMapping("{togetherId}/collections/{userId}")
    public JsonResult selectCollectionByUserId(@PathVariable Long togetherId,@PathVariable Long userId) {
        TogetherTrip togetherTrip = togetherTripService.selectCollectionByUserId(togetherId,userId);
        JsonResult result = new JsonResult();
        if (togetherTrip != null) {
            return new JsonResult();
        }
        result.mark("炸了");
        return result;
    }

    //报名时,保存结伴和user关系
    @PostMapping("{togetherId}/applys/{userId}")
    public void insertUserAndTogetherRelation(@PathVariable Long togetherId,@PathVariable Long userId){
        togetherTripService.insertUserAndTogetherRelation(togetherId,userId);
    }
    //关注时,保存结伴和user关系
    @PostMapping("{togetherId}/collections/{userId}")
    public void insertUserAndTogetherRelation1(@PathVariable Long togetherId,@PathVariable Long userId){
        togetherTripService.insertUserAndTogetherRelation1(togetherId,userId);
    }

    //取消报名时,删除结伴和user关系
    @DeleteMapping("{togetherId}/applys/{userId}")
    public void deleteTogetherAndUserRelation(@PathVariable Long togetherId,@PathVariable Long userId){
        togetherTripService.deleteTogetherAndUserRelation(togetherId,userId);
    }
    //取消关注时,删除结伴和user关系
    @DeleteMapping("{togetherId}/collections/{userId}")
    public void deleteTogetherAndUserRelation1(@PathVariable Long togetherId,@PathVariable Long userId){
        togetherTripService.deleteTogetherAndUserRelation1(togetherId,userId);
    }

    //获取该结伴已报名的用户
    @GetMapping("{togetherId}/users")
    public List<User> getUsersByTogetherId(@PathVariable Long togetherId){
        List<User> users = togetherTripService.getUsersByTogetherId(togetherId);
        return  users;
    }
    //获取该结伴已报名的用户
    @GetMapping("{togetherId}/usersCollections")
    public List<User> getUsersByTogetherCollectionId(@PathVariable Long togetherId){
        List<User> users = togetherTripService.getUsersByTogetherCollectionId(togetherId);
        return  users;
    }


    //更新报名次数
    @PutMapping({"{id}/applyNum"})
    public JsonResult updateApplyNumById(@PathVariable Long id, Integer applyNum) {
        togetherTripService.updateApplyNumById(id, applyNum);
        return new JsonResult();
    }
    //更新关注次数
    @PutMapping({"{id}/collectionNum"})
    public JsonResult updateCollectionNumById(@PathVariable Long id, Integer collectionNum) {
        togetherTripService.updateCollectionNumById(id, collectionNum);
        return new JsonResult();
    }

}
